/*
 * Copyright (c) 2017 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.serv.shop.helper;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.jnzc.ocr.common.bean.Tree;
import com.jnzc.ocr.common.error.BusinessException;
import com.jnzc.ocr.common.utils.BusinessUtils;
import com.jnzc.ocr.common.utils.TreeUtils;
import com.jnzc.ocr.serv.shop.entity.ShopProductAttr;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Set;

/**
 * @author zhangcm
 * @version 1.0, 2017/10/4
 */
public class ShopProductSkuHelper {

    private ShopProductSkuHelper(){}

    public static void check(List<Tree<ShopProductAttr>> attrList, List<Long> attrIdList){
        Set<Long> parentAttrIdSet = Sets.newHashSet();
        for (Long attrId : attrIdList) {
            ShopProductAttr attr = TreeUtils.find(attrList, attrId);
            BusinessUtils.notNull(attr, "产品属性不存在");
            BusinessUtils.notNull(attr.getParentId(), "产品属性不是子属性");
            parentAttrIdSet.add(attr.getParentId());
        }
        if(parentAttrIdSet.size() != attrIdList.size()){
            throw new BusinessException("产品属性的父属性必须不同");
        }
    }

    public static String getName(List<Tree<ShopProductAttr>> attrList, List<Long> attrIdList){
        List<String> nameList = Lists.newArrayList();
        for (Long attrId : attrIdList) {
            ShopProductAttr attr = TreeUtils.find(attrList, attrId);
            nameList.add(attr.getName());
        }
        return StringUtils.join(nameList, ",");
    }

    public static String getAttr(List<Long> attrIdList){
        if(CollectionUtils.isEmpty(attrIdList)){
            return "";
        }
        return "|" + StringUtils.join(attrIdList, "|") + "|";
    }
}
